package com.ag.zhh.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.ag.bean.TbEventGrade;
import com.ag.bean.TbEventLevel;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.lwc.service.impl.LogService;
import com.ag.wzp.action.EventMackupAction;
import com.ag.wzp.service.impl.EventBaseService;
import com.ag.wzp.service.impl.EventServiceImpl;
import com.ag.zhh.service.EventUpGradeService;

public class EventUpGradeAction extends BaseAction implements SessionAware {
	private static final Logger logger = Logger
			.getLogger(EventMackupAction.class);

	/**
	 * 查询已审核事件等级变更表byEId
	 * 
	 * @return
	 */
	public String findAuditedEventUpGradeByEId() {
		eventGrades = this.eventUpGradeService
				.findAuditedEventUpGradeByEId(eventId);
		for (TbEventGrade eventGrade : eventGrades) {
			logger.info(eventGrade.getEgId());
			logger.info(eventGrade.getEgReqTime());
		}
		return "findAuditedEventUpGrade";
	}

	/**
	 * 查询未审核事件等级变更表byEId
	 * 
	 * @return
	 */
	public String findApplyedEventUpGradeByEId() {
		eventGrade = this.eventUpGradeService
				.findApplyedEventUpGradeByEId(eventId);

		logger.info(eventGrade.getEgId());
		logger.info(eventGrade.getEgReqTime());

		return "findApplyedEventUpGrade";
	}
	
	/**
	 * 提交事件变更申请
	 * @return
	 */
	public String eventUpGrade() {
		System.out.println("EventUpGradeAction.eventUpGrade()----提交事件变更申请 事件ID:"+getEventId()+"新等级ID："+newLevelId+"申请原因："+applyContent);
		try{
		eventGrade = eventUpGradeService.eventUpGrade(getEventId(), newLevelId,
				getCurrentUser(), applyContent);
		String originLevelName = eventServiceImpl.findById(getEventId()).getTbEventLevel().getElEventLevel();
		List<TbEventLevel> listLevel = eventBaseService.getAllEventLevel();
		String newLevelName = null;
		for(TbEventLevel level: listLevel){
			if(newLevelId.equals(level.getElId())) {
				newLevelName = level.getElEventLevel();
				break;
			}
		}
		String logContent = "&nbsp;&nbsp;事件编号："+getEventId()+"\n"
								+ getCurrentUser().getUName() + " 提交事件等级变更申请：\n"
								+ "事件原等级：" +originLevelName+"    新事件等级： "+newLevelName
								+ "申请理由： \n"
								+ "    "+applyContent +"\n"
								+ "申请时间：" +eventGrade.getEgReqTime();
		logService.save(logContent, getEventId(), LogService.LOG_EVENT_LEVEL_CHANGE);
		}catch(Exception e){
			e.printStackTrace();
		}
		this.setResult("success");
		return "eventUpGrade";
	}
	
	/**
	 * 提交事件变更审核
	 * @return
	 */
	public String eventGradeAudit() {
		System.out.println("EventUpGradeAction.eventGradeAudit()----提交事件变更审核 申请ID:"+getEventId()+"是否通过："+egAgree+"审核意见："+auditContent);
		
		eventGrade = eventUpGradeService.eventUpGradeAudit(egId,
				getCurrentUser(), auditContent, egAgree.equals("true")?1:0);
		
		List<TbEventLevel> listLevel = eventBaseService.getAllEventLevel();
		String newLevelName = null;
		String oldLevelName = null;
		for(TbEventLevel level: listLevel){
			if(eventGrade.getEgNgrade().equals(level.getElId())) {
				newLevelName = level.getElEventLevel();
				continue;
			}
			if(eventGrade.getEgOgrade().equals(level.getElId())) {
				oldLevelName = level.getElEventLevel();
				continue;
			}
		}
		String logContent = "&nbsp;&nbsp;事件编号："+getEventId()+"\n"
		+ getCurrentUser().getUName() + " 提交事件等级变更审核：\n"
		+ "事件原等级：" +oldLevelName+"    新事件等级： "+newLevelName
		+ "申请理由： \n"
		+ "    "+eventGrade.getEgReqResion() +"\n"
		+ "申请时间：" +eventGrade.getEgReqTime()
		+ "审核意见： \n"
		+ "    "+auditContent +"\n"
		+ "审核时间：" +eventGrade.getEgAudTime();
		logService.save(logContent, getEventId(), LogService.LOG_EVENT_LEVEL_CHANGE);
		this.setResult("success");
		return "eventGradeAudit";
	}

	private String getEventId() {
		//return "1010011001";
		return (String) getSession()
				.getAttribute(EventUtil.PROCESSING_EVENT_ID);
	}

	private TbUser getCurrentUser() {
		user = pms.getCurrentUser(sessionMap);
		return user;
	}

	// attribute starts
	private String eventId;

	private String newLevelId;

	private String applyContent;

	private TbEventGrade eventGrade;

	private List<TbEventGrade> eventGrades;

	private String egId;

	private String auditContent;

	private String egAgree;
	
	private TbUser user;
	
	
	
	private String result;
	// attribute ends

	private Map<String, Object> sessionMap;
	
	@Resource(name = "LogService")
	private LogService logService;

	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;

	private EventUpGradeService eventUpGradeService;

	private EventBaseService eventBaseService;
	
	@Resource(name = "eventServiceImpl")
	private EventServiceImpl eventServiceImpl;

	public EventUpGradeAction() {
		super();
	}

	public EventUpGradeService getEventUpGradeService() {
		return eventUpGradeService;
	}

	public void setEventUpGradeService(EventUpGradeService eventUpGradeService) {
		this.eventUpGradeService = eventUpGradeService;
	}

	public EventBaseService getEventBaseService() {
		return eventBaseService;
	}

	public void setEventBaseService(EventBaseService eventBaseService) {
		this.eventBaseService = eventBaseService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.sessionMap = session;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public String getNewLevelId() {
		return newLevelId;
	}

	public void setNewLevelId(String newLevelId) {
		this.newLevelId = newLevelId;
	}

	public String getApplyContent() {
		return applyContent;
	}

	public void setApplyContent(String applyContent) {
		this.applyContent = applyContent;
	}

	public String getEgId() {
		return egId;
	}

	public void setEgId(String egId) {
		this.egId = egId;
	}

	public String getAuditContent() {
		return auditContent;
	}

	public void setAuditContent(String auditContent) {
		this.auditContent = auditContent;
	}


	public List<TbEventGrade> getEventGrades() {
		return eventGrades;
	}

	public void setEventGrades(List<TbEventGrade> eventGrades) {
		this.eventGrades = eventGrades;
	}

	public TbEventGrade getEventGrade() {
		return eventGrade;
	}

	public void setEventGrade(TbEventGrade eventGrade) {
		this.eventGrade = eventGrade;
	}

	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getEgAgree() {
		return egAgree;
	}

	public void setEgAgree(String egAgree) {
		this.egAgree = egAgree;
	}
}
